﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tong;
public partial class aspnet_admin_bock_keyWord_CX_keyWord_Edit : System.Web.UI.Page
{
    /// <summary>
    /// 数据操作通用类 实例 TextData
    /// </summary> 
    public TongUse TextData = new TongUse();


    /// <summary>
    /// 用户存放数据的临时表
    /// </summary>
    DataTable MyDt = new DataTable();


    /// <summary>
    /// id
    /// </summary>
    string id = "";

    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
        {
            try
            {
                id = Request.QueryString["id"].Trim();

                PageSave(int.Parse(id));
            }
            catch
            {
                Response.Write("");
            }
        }

            PageLand_First();
     
    }

    /// <summary>
    /// 加载时出现的东东
    /// </summary>
    protected void PageLand_First()
    {

        
        try
        {
            id = Request.QueryString["id"].Trim();
            int.Parse(id);
            MyDt = TextData.SqlataSet("select * from [CX_keyWord] where Cx_keyWord_id=" + id + "").Tables[0];

            Cx_keyWord_Name.Text = MyDt.Rows[0]["Cx_keyWord_Name"].ToString().Trim();
            Cx_keyWord_Sort.Text = MyDt.Rows[0]["Cx_keyWord_Sort"].ToString().Trim();
            Palen.InnerHtml = CreateCheckBox(MyDt.Rows[0]["CX_keyWord_Class_id"].ToString().Trim());
        }
        catch
        {
            MyDt = TextData.SqlataSet("select * from [CX_keyWord]  ").Tables[0];
            Cx_keyWord_Name.Text = MyDt.Rows[0]["Cx_keyWord_Name"].ToString().Trim();
            Cx_keyWord_Sort.Text = MyDt.Rows[0]["Cx_keyWord_Sort"].ToString().Trim();
            Palen.InnerHtml = CreateCheckBox(MyDt.Rows[0]["CX_keyWord_Class_id"].ToString().Trim());
        }
    }


    protected void PageSave(int id)
    {

        string Check = Request.Form["keyWord_Name"];//得到所有的分类
        if (Check==null)
        {
            tt.Text = "请选择分类";
        }
        else
        {
            try
            {
                int.Parse(Cx_keyWord_Sort.Text.Trim());
                if (Cx_keyWord_Name.Text != "" && Cx_keyWord_Name.Text.Length < 250)
                {
                    if (int.Parse(Cx_keyWord_Sort.Text.Trim()) <= 99999)
                    {
                        TextData.SqlDataSave("update [CX_keyWord] Set  Cx_keyWord_Name='" + Cx_keyWord_Name.Text.Trim() + "',Cx_keyWord_Sort = " + Cx_keyWord_Sort.Text.Trim() + ",CX_keyWord_Class_id='" + Check + "' where Cx_keyWord_id=" + id);

                        tt.Text = "更新关键字" + Cx_keyWord_Name.Text + "成功";


                    }
                    else
                    {
                        tt.Text = "排序超出范围";
                    }
                }
                else
                {
                    tt.Text = "输入的关键字为空或超过250个或不存在此关键字";

                }
            }
            catch
            {
                tt.Text = "排序超出范围";
            }

        }
    }

    /// <summary>
    /// 传入 以选的ID
    /// 创建 text box;
    /// </summary>
    /// <![CDATA[ {1,2,547,8,79,79,98,4 }    CreateCheckBox("5");
    /// ]]>
    internal string CreateCheckBox(string key_id)
    {
        string[] Cx_id;
        string str = "";
        Cx_id = key_id.Split(',');//以 ' 分开 得到数组

        DataTable KeyClass = TextData.SqlataSet("select CX_keyWord_Class_id,CX_keyWord_Class_Name from [CX_keyWord_Class]").Tables[0];
        for (int i = 0; i < KeyClass.Rows.Count; i++)
        {



            if (ISCheckBoxCreate(Cx_id, KeyClass.Rows[i]["CX_keyWord_Class_id"].ToString().Trim()))//如果选定
            {
                str += "<input id=\"keyWord_Name\" name=\"keyWord_Name\" type=\"checkbox\" value=\"" + KeyClass.Rows[i]["CX_keyWord_Class_id"].ToString().Trim() + "\" checked=\"checked\" /> " + KeyClass.Rows[i]["CX_keyWord_Class_Name"].ToString().Trim() + "   ";

            }
            else
            {
                str += "<input id=\"keyWord_Name\" name=\"keyWord_Name\" type=\"checkbox\" value=\"" + KeyClass.Rows[i]["CX_keyWord_Class_id"].ToString().Trim() + "\"  /> " + KeyClass.Rows[i]["CX_keyWord_Class_Name"].ToString().Trim() + "   ";
            }

        }
        return str;

    }

    /// <summary>
    ///  看看数组里是不是存在 这个ID
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    protected bool ISCheckBoxCreate(string[] shuZhu, string id)
    {
        for (int i = 0; i < shuZhu.Length; i++)
        {
            if (id == shuZhu[i])
            {
                return true;
            }
        }
        return false;
    }
}
